package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 养修信息预约 控制器
 */
@Controller
// 注意：访问controller的路径可以 / 开头 或 不是/ 开头, 注意要跟管理填写对应的url保持一致
@RequestMapping("business/appointment")
public class AppointmentController {
    //模板前缀
    private static final String prefix = "business/appointment/";

    @Autowired
    private IAppointmentService appointmentService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:appointment:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("system:appointment:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }



    @RequiresPermissions("system:appointment:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("appointment", appointmentService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:appointment:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Appointment> query(AppointmentQuery qo){
        return appointmentService.query(qo);
    }

    //新增
    @RequiresPermissions("system:appointment:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Appointment appointment){
        appointmentService.save(appointment);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("system:appointment:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Appointment appointment){
        appointmentService.update(appointment);
        return AjaxResult.success();
    }

    //删除(硬删除)
    @RequiresPermissions("system:appointment:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        appointmentService.deleteBatch(ids);
        return AjaxResult.success();
    }

    //删除(硬删除)
    @RequiresPermissions("system:softDelete:remove")
    @RequestMapping("/softDelete")
    @ResponseBody
    public AjaxResult softDelete(Long id){
        appointmentService.softDelete(id);
        return AjaxResult.success();
    }

    // 取消预约
    @RequiresPermissions("system:appointment:cancelAppointment")
    @RequestMapping("/cancelAppointment")
    @ResponseBody
    public AjaxResult cancelAppointment(Long id){
        appointmentService.cancelAppointment(id);
        return AjaxResult.success();
    }

    // 到店
    @RequiresPermissions("system:appointment:arrive")
    @RequestMapping("/arrive")
    @ResponseBody
    public AjaxResult arrive(Long id){
        appointmentService.arrive(id);
        return AjaxResult.success();
    }

    // 生成结算单
    @RequiresPermissions("system:appointment:generateStatement")
    @RequestMapping("/generateStatement")
    // 课堂实现
    public String generateStatement(Long id){
        // 如果为 已结算状态： 查询结算单，返回结算单id
        // 如果为 到店状态： 则根据预约单生成 结算单，在返回结算单id
        Long statementId = appointmentService.generateStatement(id);
        return "redirect:/business/statementItem/itemDetail?statementId=" + statementId;
    }


    // 查看 结算单
    @RequiresPermissions("system:appointment:selectStatement")
    @RequestMapping("/selectStatement")
    @ResponseBody
    public AjaxResult selectStatement(Long id){
        Long statementId = appointmentService.selectStatement(id);
        return AjaxResult.success(statementId);
    }

}
